<?php
/*
@author Aurelien Deharbe

This file is part of Cute-Client.

Cute-Client is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Cute-Client is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Cute-Client.  If not, see <http://www.gnu.org/licenses/>.
*/

require(dirname(__FILE__) . "/cssession.php");

class CSClient {
 
  var $serversocket;
  
  function __construct($host, $port) {
    $this->serversocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    socket_connect($this->serversocket, $host, $port);
  }
  
  function newSession($sessid) {
    return $this->accessSession("QSSessionNew(".$sessid.")\n");
  }
  
  function joinSession($sessid) {
    return $this->accessSession("QSSessionJoin(".$sessid.")\n");
  }
  
  function accessSession($cmd) {
    if (!isset($this->serversocket)) {
      return NULL;
    }
    socket_write($this->serversocket, $cmd, strlen($cmd));
    socket_read($this->serversocket, 256);
    $session = new CSSession();
    $sessport = $session->getPort();
    $cmd = "QSSessionPort(".$sessport.")\n";
    socket_write($this->serversocket, $cmd, strlen($cmd));
    socket_read($this->serversocket, 256);
    $session->accept();
    return $session;
  }
}
?>